﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Drawing;

namespace TLD_LPR.Segmentation
{
    public partial class SegmentationAlgorithm : TLD_LPR.AlgorithmBase.VisionAlgorithm
    {
        List<Bitmap> Characterlist = new List<Bitmap>();
        public SegmentationAlgorithm(String name) : base(name) { }
        public List<Bitmap> BB
        {
            get
            {
                return Characterlist;
            }
        }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Characterlist = new List<Bitmap>();
            List<Blob> blobbers = new List<Blob>();

            blobbers = BlobDetection.doAlgorithm(sourceImage);
            int asijfi = 0;

            blobbers.Sort((x, y) => x.minx.CompareTo(y.minx));
            int pixels = sourceImage.Width * sourceImage.Height;
            foreach (Blob b in blobbers)
            {
                if (b.amountOfPixels < pixels / 1150)
                    continue;
                if (b.maxx - b.minx < 5)
                    continue;

                if (!(Math.Abs(b.maxx - b.minx) > Math.Abs(b.maxy - b.miny)))
                {
                    PixelPoint tempx = new PixelPoint(b.minx, b.miny, 231, 0, 0, 0, null);
                    PixelPoint tempy = new PixelPoint(b.maxx, b.maxy, 231, 0, 0, 0, null);

                    try
                    {
                        Bitmap temp = (sourceImage).crop(b.minx, b.maxx, b.miny, b.maxy);
                        Characterlist.Add(temp);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Alles gaat hier goed hoor *whistle whistle*");
                    }
                    asijfi++;
                }
            }
            if (Characterlist.Count == 0)
            {
                foreach (Blob b in blobbers)
                {
                    if (b.amountOfPixels < pixels / 2500)
                        continue;
                    if (b.maxx - b.minx < 5)
                        continue;
                    if (!(Math.Abs(b.maxx - b.minx) > Math.Abs(b.maxy - b.miny)))
                    {
                        PixelPoint tempx = new PixelPoint(b.minx, b.miny, 231, 0, 0, 0, null);
                        PixelPoint tempy = new PixelPoint(b.maxx, b.maxy, 231, 0, 0, 0, null);

                        try
                        {
                            Bitmap temp = (sourceImage).crop(b.minx, b.maxx, b.miny, b.maxy);
                            Characterlist.Add(temp);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Alles gaat hier goed hoor *whistle whistle*");
                        }
                        asijfi++;
                    }
                }
            }
            Console.WriteLine("Aantal blobs: " + Characterlist.Count);
            return sourceImage;
        }
    }
}
